const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
const jwt = require('jsonwebtoken')
const bcryptjs = require('bcryptjs')
    /*
        如何获取值 get请求方法   获取req的query方法取值
                   post请求方法  获取req的body方法取值
    */
module.exports = {
    //登录
    async postLogin(req, res) {
        //查表
        //返回token 
        let data = req.body;
        console.log("----data---", req.body)
        knex("user_tab").select().where({ username: data.username }).then(resp => {
            //let isPassword = bcryptjs.compareSync(data.password, resp[0].password)
            let isPassword = 1;
            console.log("-----isPassword----", isPassword);
            if (resp.length === 0) {
                console.log('用户不存在-------')
                res.send({
                    code: 3003,
                    msg: "用户不存在"
                })
            } else if (!isPassword) {
                console.log('密码错误`````')
                return res.send({
                    code: 3004,
                    msg: "密码错误"
                })
            } else {
                const token = jwt.sign({ id: resp.id }, "sha");
                console.log('登陆成功-------')
                return res.send({
                    code: 3002,
                    data: resp,
                    msg: "登陆成功",
                    token: token
                })
            }
        })
    },
    async postRegister(req, res) {
        //注册
        console.log("---req.body.data---", req.body)
        let { username, password } = req.body;
        if (username.length < 4) {
            res.send({
                code: 4002,
                msg: "账号长度小于6！",
                success: true
            })
        } else if (password.length < 6) {
            res.send({
                code: 4003,
                msg: "密码长度小于6！",
                success: true
            })
        } else {
            knex('user_tab').where({
                username: username
            }).limit(1).then(resq => {
                console.log("---resq---", resq)
                if (resq.length > 0) {
                    res.send({
                        code: 4004,
                        msg: "用户已存在！",
                        success: true
                    })
                } else {
                    //-------------- 缺少password加密 -----------------
                    let hashPassword = bcryptjs.hashSync(password, 10);
                    knex('user_tab').insert({
                        username: username,
                        password: hashPassword,
                    }).then(resp => {
                        console.log("res", resp);
                        if (resp) {
                            const token = jwt.sign({ id: resp.id }, "sha");
                            res.send({
                                code: 200,
                                msg: "操作成功",
                                success: true,
                                token: token
                            })
                        } else {
                            res.send({
                                code: 4001,
                                msg: "操作失败",
                                success: true
                            })
                        }
                    })
                }
            })
        }
    }
}